package com.yuke.cloud.service.uac.service.impl;

import com.google.common.base.Preconditions;
import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.service.api.uac.dto.GroupInfoDto;
import com.yuke.cloud.service.api.uac.exceptions.UacBizException;
import com.yuke.cloud.service.uac.dao.AccGroupMapper;
import com.yuke.cloud.service.uac.dao.AccUserGroupMapper;
import com.yuke.cloud.service.uac.entity.AccGroup;
import com.yuke.cloud.service.uac.entity.AccUserGroup;
import com.yuke.cloud.service.uac.service.AccGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * Created by wg on 2018/09/15.
 */
@Service
@Transactional
public class AccGroupServiceImpl extends BaseService<AccGroup> implements AccGroupService {
    @Resource
    private AccGroupMapper accGroupMapper;

    @Resource
    private AccUserGroupMapper accUserGroupMapper;

    //保存组织
    public int saveGroup(AccGroup group) {

        int result;
        Preconditions.checkArgument(!StringUtils.isEmpty(group.getParentId()), "上级节点不能为空");

        AccGroup parentGroup = accGroupMapper.selectByPrimaryKey(group.getParentId());
        if (PublicUtil.isEmpty(parentGroup)) {
            throw new UacBizException(ErrorCodeEnum.UAC10015009, group.getParentId());
        }

        if (null == group.getGroupId()) {  //新增组织
            Long groupId = super.generateId();
            group.setGroupId(groupId);
            group.setLevel(parentGroup.getLevel() + 1);
            result = this.save(group);
        } else {
            result = this.update(group);
        }
        return result;
    }

    public int deleteGroupById(Long groupId) {

        Preconditions.checkArgument(groupId != null, "组织id为空");
        Preconditions.checkArgument(!Objects.equals(groupId, GlobalConstant.Sys.SUPER_MANAGER_GROUP_ID), "该组织不能删除");
        Preconditions.checkArgument(!Objects.equals(groupId, GlobalConstant.Sys.GROUP_COMPANY), "该组织不能删除");
        Preconditions.checkArgument(!Objects.equals(groupId, GlobalConstant.Sys.GROUP_SUPPLIER), "该组织不能删除");
        Preconditions.checkArgument(!Objects.equals(groupId, GlobalConstant.Sys.GROUP_SHOPPER), "该组织不能删除");
        Preconditions.checkArgument(!Objects.equals(groupId, GlobalConstant.Sys.GROUP_CUSTOMER), "该组织不能删除");

        // 根据前台传入的组织参数校验该组织是否存在
        AccGroup accGroup = accGroupMapper.selectByPrimaryKey(groupId);
        if (PublicUtil.isEmpty(accGroup)) {
            throw new UacBizException(ErrorCodeEnum.UAC10015004, groupId);
        }

        //判断该组织下是否存在子节点
        AccGroup childAccGroup = new AccGroup();
        childAccGroup.setParentId(groupId);
        List<AccGroup> childGroupList = accGroupMapper.select(childAccGroup);
        if (PublicUtil.isNotEmpty(childGroupList)) {
            throw new UacBizException(ErrorCodeEnum.UAC10015007, groupId);
        }

        //判断该组织下是否存在用户
        Example example = new Example(AccUserGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("groupId", groupId);
        int result = accUserGroupMapper.selectCountByExample(example);
        if (result > 0) {
            throw new UacBizException(ErrorCodeEnum.UAC10015008, groupId);
        }

        return mapper.deleteByPrimaryKey(groupId);
    }

    public List<AccGroup> getChildGroups(Long parentId) {
        Preconditions.checkArgument(parentId != null, "组织id为空");

        Example example = new Example(AccGroup.class);
        example.setOrderByClause("group_name");
        Example.Criteria criteria = example.createCriteria();
        //todo 20200404 修改为获取全部2级组织
        //
        if (parentId > 1){
            criteria.andEqualTo("parentId", parentId);
        }else{
            criteria.andEqualTo("level",2);
        }

        List<AccGroup> list = accGroupMapper.selectByExample(example);

        return list;
    }

    /**
     *  根据groupId递归找出父节点
     * @param groupId
     * @return
     */
    public List<AccGroup> queryGroupListRecurDesc(Long groupId) {
        Preconditions.checkArgument(groupId != null, "组织id为空");

        List<AccGroup> list = accGroupMapper.queryGroupListRecurDesc(groupId);

        return list;
    }

    public GroupInfoDto getGroupInfoByGroupId(Long groupId){
        GroupInfoDto groupInfoDto = accGroupMapper.getGroupInfoByGroupId(groupId);

        return groupInfoDto;

    }
}
